<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务，帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件，不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types = 1);

namespace app\api\controller\ding\order;

use app\api\controller\Controller;
use app\api\model\ding\Order as OrderModel;
use app\api\model\ding\OrderGoods as OrderGoodsModel;
use app\api\service\User as UserService;
use app\common\service\qrcode\Extract as ExtractQRcode;
use think\facade\Db;
use think\response\Json;

/**
 * 用户预订管理
 * Class Comment
 * @package app\api\controller\order
 */
class Index extends Controller
{

    /**
     * 获取自提核销二维码
     * @param int $orderId
     * @param string $channel 二维码渠道(小程序码、h5码)
     * @return Json
     * @throws BaseException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function extractQrcode(int $orderId, string $channel = 'H5'): Json
    {
        // 订单详情
        $order = OrderModel::getDetail($orderId);

        // 当前用户信息
        $userInfo = UserService::getCurrentLoginUser(true);
        // 创建二维码
        $Qrcode = new ExtractQRcode($userInfo, $orderId, $channel);
        return $this->renderSuccess(['qrcode' => $Qrcode->getImage()]);
    }
    //订单邀请函，访问记录
    public function addView(int $orderId): Json
    {
        // 订单详情
        $order               = OrderModel::getDetail($orderId);
        $data['order_id']    = $orderId;
        $data['shop_id']     = $order['shop_id'];
        $data['clerk_id']    = $order['clerk_id'];
        $data['create_time'] = time();
        $data['store_id']    = 10001;
        Db::name('ding_order_view')->insert($data);

        return $this->renderSuccess([]);
    }
    public function queuMenu(array $menuIds = [])
    {
        $model = new OrderGoodsModel;
        if (! $model->sUpdate($menuIds)) {
            return $this->renderError($model->getError() ?: '操作失败');
        }

        return $this->renderSuccess([], '操作成功');
    }
    /*
		*预订列表，员工查看
	*/

    public function yudingList(string $status, int $shopId = 0)
    {
        $userId = UserService::getCurrentLoginUserId();
        $model  = new OrderModel;
        $list   = $model->getListYuding($status, $shopId);
        return $this->renderSuccess(compact('list'));
    }

    /**
     * 我的订单列表
     * @param string $dataType 订单类型 (all全部 payment待付款 received待发货 deliver待收货 comment待评价)
     * @return Json
     * @throws \think\db\exception\DbException
     * @throws BaseException
     */
    public function list(string $dataType)
    {
        $userId = UserService::getCurrentLoginUserId();
        $model  = new OrderModel;
        $list   = $model->getListUser($dataType);
        //待使用
        $wait_count = Db::name('ding_order')->where('user_id', $userId)->where('order_status', 10)->count();
        return $this->renderSuccess(compact('list', 'wait_count'));
    }
    /*
		*按菜单，商品列表
	*/
    public function goodsList(int $orderId)
    {
        $model = new OrderGoodsModel;
        $list  = $model->getList($orderId);
        return $this->renderSuccess(compact('list'));
    }
    /*
		*订单全部商品列表
	*/
    public function goodsListAll(int $orderId)
    {
        $model = new OrderGoodsModel;
        $list  = $model->getListAll($orderId);
        return $this->renderSuccess(compact('list'));
    }
    /*
		*订单全部商品列表-待确认
	*/
    public function goodsListAllWait(int $orderId)
    {
        $model = new OrderGoodsModel;
        $list  = $model->getListAllWait($orderId);
        return $this->renderSuccess(compact('list'));
    }
    /**
     * 菜单确认-用户
     * @param int $orderId
     * @return array|\think\response\Json
     * @throws \cores\exception\BaseException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function caidanQueu(int $orderId)
    {
        // 订单信息
        $orderInfo = OrderModel::detail($orderId);

        $orderInfo->caidanQueu($this->postForm());
        return $this->renderSuccess([], '提交成功');

    }
    /**
     * 创建菜单-店员
     * @param int $orderId
     * @return array|\think\response\Json
     * @throws \cores\exception\BaseException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function diancaiClerk(int $orderId)
    {

        $model = new OrderGoodsModel;
        if ($model->increasedClerk($orderId, $this->postForm())) {
            return $this->renderSuccess([], '提交成功');
        }
        return $this->renderError($model->getError() ?: '失败');
    }
    /**
     * 创建菜单-用户
     * @param int $orderId
     * @return array|\think\response\Json
     * @throws \cores\exception\BaseException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function submit(int $orderId)
    {
        // 订单信息
        $orderInfo = OrderModel::detail($orderId);

        $dd               = $this->postForm();
        $dd['show_price'] = Db::name('store_shop')->where('shop_id', $orderInfo['shop_id'])->value('is_show');

        $orderInfo->updateOrder($dd);

        $model = new OrderGoodsModel;
        if ($model->increased($orderId, $this->postForm())) {
          
            return $this->renderSuccess([], '提交成功');
        }
        return $this->renderError($model->getError() ?: '失败');
    }

    public function cancelUser(int $orderId, string $type)
    {
        $detail = OrderModel::getDetail($orderId);

        if ($detail->cancelUser($type)) {
            return $this->renderSuccess([], '已处理');
        }
        return $this->renderError($model->getError() ?: '取消失败');
    }

    public function cancel(int $orderId, string $type)
    {
        $detail = OrderModel::getDetail($orderId);

        if ($detail->cancel($type)) {
            return $this->renderSuccess([], '处理成功');
        }
        return $this->renderError($model->getError() ?: '取消失败');
    }

    /**
     * 立即支付
     * @param int $orderId 订单ID
     * @param int $payType 支付方式
     * @return Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws BaseException
     */
    public function pay(int $orderId, int $payType = OrderPayTypeEnum::WECHAT)
    {
        // 获取订单详情
        $model = OrderModel::getDetail($orderId);

        // 订单支付事件
        if (! $model->onPay($payType)) {
            return $this->renderError($model->getError() ?: '订单支付失败');
        }
        // 构建微信支付请求
        $payment = $model->onOrderPayment($model, $payType);
        // 支付状态提醒
        return $this->renderSuccess([
            'order_id' => $model['id'], // 订单id
            'pay_type' => $payType,     // 支付方式
            'payment'  => $payment,     // 微信支付参数
        ]);
    }

}
